'''
@Project ：hdcms-scripts
@File    ：create_live_config.py
@IDE     ：PyCharm
@Author  ：朱广杰
@Date    ：2024/12/2 18:26
'''
import jsonpath
from common.hd_log import mylog
from common.FILE_PATH import get_config_value
from script.api.course.api_live_room import GetLiveInfoApi
from script.api.course.create_live_config import AddPopApi, AddInteractApi, GetInteractApi, UpdateInteractStateApi, \
    AddPrizeApi, GetPrizeApi, UpdatePrizeApi, AddChoiceQuestionApi, GetChoiceQuestionApi, AddQuestionApi, \
    GetQuestionApi, PushQuestionApi


def get_room_id(course_id):
    try:
        room_info = GetLiveInfoApi(course_id).send()
        room_id = jsonpath.jsonpath(room_info.json(), '$..room_id')[0]
        mylog.info(f'根据课程ID{course_id}获取到直播间ID是{room_id}')
        return room_id
    except Exception as e:
        mylog.error(f'根据课程ID{course_id}获取直播间ID失败,错误信息是：{e}')

def add_pop(course_id, room_id):
    # 读取测试数据
    pop_types = ['move_pop', 'default_pop', 'live_make_pop', 'join_course_pop', 'live_room_pop']

    for pop_type in pop_types:
        mylog.info(f'开始添加{pop_type}弹窗')
        try:
            pop_config = get_config_value('/config/common.yml', pop_type)
            resp = AddPopApi(pop_type=pop_config.get('pop_type'),
                           course_id=course_id,
                           chatroom_id=room_id,
                           collect_course_info=pop_config.get('collect_course_info'),
                           extra_info=pop_config.get('extra_info')).send()
            resp_json = resp.json()
            code = resp_json['error_no']
            assert code == 0
        except Exception as e:
            mylog.error(f'添加{pop_type}弹窗失败,错误信息是：{e}')
        mylog.info(f'添加{pop_type}弹窗成功')

def add_interact(course_id, room_id):
    mylog.info(f'开始添加course_id为{course_id}的呼应time')
    try:
        resp = AddInteractApi(course_id=course_id, chatroom_id=room_id).send()
        resp_json = resp.json()
        code = resp_json['error_no']
        assert code == 0
        mylog.info(f'添加course_id为{course_id}的呼应time成功')
    except Exception as e:
        mylog.error(f'添加course_id为{course_id}的呼应time失败,错误信息是：{e}')

def get_interact_id(room_id):

    try:
        resp = GetInteractApi(chatroom_id=room_id).send()
        interact_id = jsonpath.jsonpath(resp.json(), '$..interact_id')[0]
        mylog.info(f'根据room_id{room_id}获取的interact_id是{interact_id}')
        return interact_id
    except Exception as e:
        mylog.error(f'根据room_id{room_id}获取的interact_id失败,错误信息是：{e}')

def update_interact(interact_id):
    try:
        resp = UpdateInteractStateApi(interact_id).send()
        resp_json = resp.json()
        code = resp_json['error_no']
        assert code == 0
        mylog.info(f'发送interact_id为{interact_id}的呼应time成功')
    except Exception as e:
        mylog.error(f'发送interact_id为{interact_id}的呼应time失败,错误信息是：{e}')

def add_prize(room_id):
    try:
        resp = AddPrizeApi(room_id).send()
        resp_json = resp.json()
        code = resp_json['error_no']
        assert code == 0
        mylog.info(f'新增 room_id 为{room_id}的红包成功')
    except Exception as e:
        mylog.error(f'新增 room_id 为{room_id}的红包失败,错误信息是：{e}')

def get_prize_id(room_id):

    try:
        resp = GetPrizeApi(chatroom_id=room_id).send()
        prize_id = jsonpath.jsonpath(resp.json(), '$..prize_id')[0]
        mylog.info(f'根据room_id{room_id}获取的prize_id是{prize_id}')
        return prize_id
    except Exception as e:
        mylog.error(f'根据room_id{room_id}获取的prize_id失败,错误信息是：{e}')

def update_prize(prize_state, prize_id):
    try:
        resp = UpdatePrizeApi(prize_state=prize_state, prize_id=prize_id).send()
        resp_json = resp.json()
        code = resp_json['error_no']
        assert code == 0
        mylog.info(f'开启/关闭 prize_id 为{prize_id}的红包成功')
    except Exception as e:
        mylog.error(f'开启/关闭 prize_id 为{prize_id}的红包失败,错误信息是：{e}')

def add_choice_question(course_id, room_id):
    try:
        resp = AddChoiceQuestionApi(course_id, room_id).send()
        resp_json = resp.json()
        code = resp_json['error_no']
        assert code == 0
        mylog.info(f'新增 room_id 为{room_id}的题目成功')
    except Exception as e:
        mylog.error(f'新增 room_id 为{room_id}的题目失败,错误信息是：{e}')

def get_question_id(course_id,room_id):

    try:
        resp = GetChoiceQuestionApi(course_id=course_id, chatroom_id=room_id).send()
        question_id = jsonpath.jsonpath(resp.json(), '$..interact_id')[0]
        mylog.info(f'根据room_id{room_id}获取的question_id是{question_id}')
        return question_id
    except Exception as e:
        mylog.error(f'根据room_id{room_id}获取的question_id失败,错误信息是：{e}')


def add_nps(course_id, room_id):
    try:
        resp = AddQuestionApi(course_id,room_id).send()
        resp_json = resp.json()
        code = resp_json['error_no']
        assert code == 0
        mylog.info(f'新增 room_id 为{room_id}的nps成功')
    except Exception as e:
        mylog.error(f'新增 room_id 为{room_id}的nps失败,错误信息是：{e}')

def get_nps_id(course_id,room_id):

    try:
        resp = GetQuestionApi(course_id,room_id).send()
        nps_id = jsonpath.jsonpath(resp.json(), '$..question_id')[0]
        mylog.info(f'根据room_id{room_id}获取的nps_id是{nps_id}')
        return nps_id
    except Exception as e:
        mylog.error(f'根据room_id{room_id}获取的nps_id失败,错误信息是：{e}')

def update_nps(nps_id, course_id,room_id):
    try:
        resp = PushQuestionApi(nps_id,course_id,room_id).send()
        resp_json = resp.json()
        code = resp_json['error_no']
        assert code == 0
        mylog.info(f'开启/关闭 nps_id 为{nps_id}的nps弹窗成功')
    except Exception as e:
        mylog.error(f'开启/关闭 nps_id 为{nps_id}的nps弹窗失败,错误信息是：{e}')

if __name__ == '__main__':
    course_id = '70a462c9be90bab4700317f34f879888'
    room_id = get_room_id(course_id)
    add_pop(course_id, room_id)
    add_interact(course_id, room_id)
    interact_id = get_interact_id(room_id)
    update_interact(interact_id)
    prize_id = get_prize_id(room_id)
    update_prize(0, prize_id)
    add_prize(room_id)
    prize_id = get_prize_id(room_id)
    update_prize(1, prize_id)
    add_choice_question(course_id, room_id)
    question_id = get_question_id(course_id, room_id)
    update_interact(question_id)
    add_nps(course_id, room_id)
    nps_id = get_nps_id(course_id, room_id)
    update_nps(nps_id, course_id, room_id)

